/*
 * 
 * 
 * 
 */
package com.cms.controller.admin;

import com.cms.constant.CommonConstant;
import com.cms.utils.PathUtils;
import com.cms.utils.TemplateUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.io.IOException;

/**
 * Controller - 模板
 * 
 * 
 * 
 */
@Controller("adminTemplateController")
@RequestMapping("/admin/template")
public class TemplateController extends BaseController {
	
	/**
	 * 列表
	 */
	@RequestMapping
	public String index(String directory, ModelMap modelMap) {
		setListQuery();
		if(StringUtils.isNotBlank(directory)){
			modelMap.addAttribute("templateFiles", TemplateUtils.getTemplateFiles(directory.replaceAll(",", "/")));
			modelMap.addAttribute("directory", directory);
		}else{
			modelMap.addAttribute("templateFiles", TemplateUtils.getSiteTemplates(getCurrentSite()));
		}
		return getView("template/index");
	}
	
	/**
	 * 添加
	 */
	@RequestMapping("/add")
	public String add(String directory, ModelMap modelMap) {
		modelMap.addAttribute("directory", directory);
		return getView("template/add");
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public String save(String fileName,String content,String directory) {
		String filePath = "";
		if(StringUtils.isNotBlank(directory)){
			filePath = "/"+directory.replaceAll(",", "/")+"/"+fileName;
		}else{
			filePath = "/"+fileName;
		}
		try {
			FileUtils.write(new File(PathUtils.getWebRootPath()+"/templates/"+filePath), content);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:"+getListQuery("/admin/template");
	}

	/**
	 * 查看
	 */
	@RequestMapping("/view")
	public String view(String fileName,String directory) {
		if (StringUtils.isBlank(fileName)) {
			return CommonConstant.ADMIN_ERROR_VIEW;
		}
		String filePath = "";
		if(StringUtils.isNotBlank(directory)){
			filePath = "/"+directory.replaceAll(",", "/")+"/"+fileName;
		}else{
			filePath = "/"+fileName;
		}
		return "redirect:/templates/"+filePath;
	}
	
	/**
	 * 编辑
	 */
	@RequestMapping("/edit")
	public String edit(String fileName,String directory, ModelMap modelMap) {
		if (StringUtils.isBlank(fileName)) {
			return CommonConstant.ADMIN_ERROR_VIEW;
		}
		modelMap.addAttribute("directory", directory);
		modelMap.addAttribute("fileName", fileName);
		String filePath = "";
		if(StringUtils.isNotBlank(directory)){
			filePath = "/"+directory.replaceAll(",", "/")+"/"+fileName;
		}else{
			filePath = "/"+fileName;
		}
		modelMap.addAttribute("content", StringEscapeUtils.escapeHtml(TemplateUtils.read(filePath)));
		return getView("template/edit");
	}

	/**
	 * 更新
	 */
	@RequestMapping("/update")
	public String update(String fileName,String directory,String content) {
		if (StringUtils.isBlank(fileName) || content == null) {
			return CommonConstant.ADMIN_ERROR_VIEW;
		}
		String filePath = "";
		if(StringUtils.isNotBlank(directory)){
			filePath = "/"+directory.replaceAll(",", "/")+"/"+fileName;
		}else{
			filePath = "/"+fileName;
		}
		TemplateUtils.write(filePath, content);
		return "redirect:"+getListQuery("/admin/template");
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public String delete(String fileName,String directory) {
		String filePath = "";
		if(StringUtils.isNotBlank(directory)){
			filePath = "/"+directory.replaceAll(",", "/")+"/"+fileName;
		}else{
			filePath = "/"+fileName;
		}
		TemplateUtils.delete(filePath);
		return "redirect:"+getListQuery("/admin/template");
	}

}